package com.pass.util.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

import com.pass.util.R;

import java.util.Objects;

public class SimpleDialog extends DialogFragment {
    private DialogCallback callback;
    private String sureStr;
    private String cancelStr;
    private String contentStr;
    private boolean canceledOnTouchOutside;


    /**
     * 初始化器
     *
     * @param canceledOnTouchOutside 点击外部是否关闭Dialog
     */
    public SimpleDialog(boolean canceledOnTouchOutside, DialogCallback dialogCallback, String sureStr, String cancelStr, String contentStr) {
        this.canceledOnTouchOutside = canceledOnTouchOutside;
        callback = dialogCallback;
        this.sureStr = sureStr;
        this.cancelStr = cancelStr;
        this.contentStr = contentStr;
    }

    /**
     * 仅仅提示一下，不需要回调的快速方法
     *
     * @return
     */
    public SimpleDialog(boolean canceledOnTouchOutside, String contentStr) {
        this.canceledOnTouchOutside = canceledOnTouchOutside;
        this.sureStr = "确定";
        this.cancelStr = "";
        this.contentStr = contentStr;
    }

    public SimpleDialog(String contentStr) {
        this.canceledOnTouchOutside = true;
        this.sureStr = "确定";
        this.cancelStr = "";
        this.contentStr = contentStr;
    }

    public SimpleDialog(String contentStr, Boolean canceledOnTouchOutside) {
        this.canceledOnTouchOutside = canceledOnTouchOutside;
        this.sureStr = "确定";
        this.cancelStr = "";
        this.contentStr = contentStr;
    }


    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
        builder.setMessage(this.contentStr).setPositiveButton(this.sureStr, (dialog, which) -> {
            if (null != callback) {
                callback.onOKClicked();
            } else {
                dialog.dismiss();
            }
        });
        if (!TextUtils.isEmpty(this.cancelStr)) {
            builder.setNegativeButton(this.cancelStr, (dialog, which) -> {
                if (null != callback) {
                    callback.onCancelled();
                } else {
                    dialog.dismiss();
                }
            });
        }
        setCancelable(this.canceledOnTouchOutside);
        return builder.create();
    }

}
